document.write('<script src=Include/Core/JS/jquery.js></script>');
document.write('<script src=Include/Core/JS/jquery.cookie.js></script>');
document.write('<script src=Include/Core/JS/jquery.ui.js></script>');
document.write('<script src=Include/Core/JS/jquery.ui.i18n.js></script>');
document.write('<script src=Include/Core/JS/jquery.ui.window.js></script>');
document.write('<script src=Include/Core/JS/jquery.ui.combobox.js></script>');

// contentArr[0] = full content
// return window content
function _FindoutWindowContent(winTag, contentArr)
{
    var content = contentArr[0];
    var firstPos = content.indexOf(winTag);
    var lastPos = content.lastIndexOf(winTag);
    
    if( firstPos == -1 || lastPos == -1 ){
        return '';
    }
    
    var leftStr = content.substr(0, firstPos);            // left
    var rightStr = content.substr(lastPos+winTag.length);  // right
    var winContent =  content.substr(
        firstPos+winTag.length, lastPos-(firstPos+winTag.length));
    contentArr[0] = leftStr + rightStr;
    
    return winContent;
}

function FindoutWindowContent(responseText, isPre, returnContent)
{
    var matchStr;
    if( isPre ){
        matchStr= /<!--#PW#[^<>]*?#PW#-->/gi;
    }else{
        matchStr= /<!--#W#[^<>]*?#W#-->/gi;
    } 
    var winAllTagList  = responseText.match(matchStr);
    
    // error
    if( winAllTagList == null ){
        if( !isPre ){
            $('body').append(responseText);
        }else{
            returnContent[0] = responseText;
        }
        return null;
    }
    
    var winTagList = new Array();
    var alreadyHas = false;
    for(var i=0; i<winAllTagList.length; ++i)
    {
        alreadyHas = false;
        for(var j=0; j<winTagList.length; ++j)
        {
            if(winAllTagList[i] == winTagList[j])
            {
                alreadyHas = true;
                break;
            }
        }
        if( !alreadyHas )
        {
            winTagList[winTagList.length] = winAllTagList[i];
        }
    }
    
    var winList = new Array();
    var contentTempArr = new Array();
    contentTempArr[0] = responseText;
    var winContent, curWinTag;
    for(var i=0; i<winTagList.length; ++i)
    {
        winContent = '';
        curWinTag = winTagList[i];
        for(var key in winList )
        {
            var newContentTempArr = new Array();
            newContentTempArr[0] = winList[key];
            winContent += _FindoutWindowContent(curWinTag, newContentTempArr);
            winList[key] = newContentTempArr[0];
        }
        winContent += _FindoutWindowContent(curWinTag, contentTempArr);
        winList[curWinTag] = winContent;
    }
    
    if( isPre ){
        returnContent[0] = contentTempArr[0];
    }
    
    return winList;
}

function AjaxProcess(responseText, defaultWindow)
{
    if( !responseText || responseText.length <= 0 ){
        return;
    }
    if( !defaultWindow ){ defaultWindow = 'body'; }
    
    var winList = FindoutWindowContent(responseText, false);

    var winContent, winTempStr, winTempArr, winName, execMethod, 
        preWinContent, preWinTempStr, preWinTempArr, preWinName, preExecMethod,
        preWinList;
     
    for( var winTag in winList )
    {
        // find out window name, execute method, window content
        winFullContent = winList[winTag];
        winTempStr = winTag.substr(7, winTag.length - 13);
        winTempArr = winTempStr.split('||');
        winName = winTempArr[0];
        execMethod = winTempArr[1].toLowerCase();
        
        // adjust target window name
        if(winName.length <= 0){
            winName = defaultWindow;
        }else{
            if( winName != 'body' && 
                winName != 'document' &&
                winName != 'window')
            {
                winName = '#'+winName;
            }
        }
        
        // adjust method
        if(execMethod.length <= 0){
            execMethod   = 'html';
        }
        
        // get window pre content
        var preContentArr = new Array();
        preContentArr[0] = '';
        var preWinList = FindoutWindowContent(winFullContent, true, preContentArr);
        winContent = preContentArr[0];
        preWinContent = '';
        //alert(winTag + '\r\n' + winContent);
        
        if( preWinList !== null )
        {
            // only one normally
            var preCount = 0;
            for( var preWinTag in preWinList )
            {
                preCount++;
                // get previously content, previously window name, previously window execute method
                preWinTempStr = preWinTag.substr(8, preWinTag.length-15);
                preWinTempArr = preWinTempStr.split('||');
                preWinName  = preWinTempArr[0];
                preExecMethod = preWinTempArr[1].toLowerCase();
                
                preWinContent = preWinList[preWinTag];
                
                 // adjust previously target window name
                if(preWinName.length <= 0){
                    preWinName = defaultWindow;
                }else{
                    if( preWinName != 'body' && 
                        preWinName != 'document' &&
                        preWinName != 'window')
                    {
                        preWinName = '#'+preWinName;
                    }
                }
                // adjust previously method
                if(preExecMethod.length <= 0){
                    preExecMethod   = 'html';
                }
            }
            
            if( preCount.length > 1 )
            {
                alert('Warning:\r\n\r\n'+
                    'One view only have one previously content!\r\n\r\n'+
                    'May be some previously content position is error.');
            }
        }
        
        if( preWinContent.length > 0 )
        {
            //alert(preWinName+'.'+preExecMethod+'\n'+preContent);
            // render previously content
            switch(preExecMethod)
            {
                case 'html':
                    $(preWinName).html(preWinContent);
                break;
                case 'append':
                    $(preWinName).append(preWinContent);
                break;
                default:
                    $(preWinName).text(preWinContent);
                break;
            }
        }
        
        //alert(winName+'.'+execMethod+'\n'+content);
        // render content
        switch(execMethod)
        {
            case 'html':
                $(winName).html(winContent);
            break;
            case 'append':
                $(winName).append(winContent);
            break;
            default:
                $(winName).text(winContent);
            break;
        }
    }
    //alert(String(new Date().getTime()-st)+' 毫秒');
}

function SwitchThemes(themesPath)
{
    //$("link.ui-theme").remove();
    var cssLink = $('<link href="' + themesPath +
        '" type="text/css" rel="Stylesheet" class="ui-theme" />');
    $("head").append(cssLink);
    
    if( $("link.ui-theme").size() > 3 ){
        $("link.ui-theme:first").remove();
    }
}